package spacecrack;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Unique;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(detachable = "true")
public class Player {
	public static Key getKey(String id) {
		if (id == null) throw new IllegalArgumentException();
		if (id.isEmpty()) throw new IllegalArgumentException();

		return KeyFactory.createKey(Player.class.getSimpleName(), id);
	}

	@PrimaryKey
	@Persistent
	private Key key;

	@Persistent
	@Unique
	private String name;

	public Player(String id, String name) {
		this.key = getKey(id);
		this.name = name;
	}

	public Key getKey() { return key; }
	public String getName() { return name; }

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "[Player '"+ key.getName() +"' '"+ name +"']";
	}
}
